<template>
  <div class="about">
    <vue-particles
      color="#fff"
      :particleOpacity="0.7"
      :particlesNumber="300"
      shapeType="star"
      :particleSize="4"
      linesColor="#FFFFFF"
      :linesWidth="0"
      :lineLinked="true"
      :lineOpacity="0.2"
      :linesDistance="80"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
      class="cash"
    >
    </vue-particles>
    <div class="box">
      <div class="back">
        <el-form
          class="form"
          label-position="left"
          :model="form"
          :rules="rules"
          ref="form"
        >
          <p class="title">M A C 后台管理系统</p>
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              placeholder="请输入管理员账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入管理员密码"
              @keyup.native.enter="onSubmit"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              style="width: 100%"
              @click="onSubmit()"
              @keyup.native.enter="onSubmit"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>


<script>
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请填写用户名", trigger: "blur" },
          {
            pattern: /^\w{3,15}$/,
            message: "3~15位字符，可以包含：数字、字母、下划线",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请填写用户名", trigger: "blur" },
          {
            pattern: /^\w{6,15}$/,
            message: "6~15位字符，可以包含：数字、字母、下划线",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    onSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // 验证通过
          // 发送登录请求
          this.$http.adminApi.login(this.form).then((res) => {
            if (res.data.code == 200) {
              // 跳转到首页
              this.$message({
                type: "success",
                message: "登陆成功",
              });
              // 将登录用户存入Vuex
              this.$store.commit("updateUserInfo", res.data.data.user);
              this.$store.commit("saveToken", res.data.data.token);
              console.log(res);
              this.$router.push("/home/index");
            } else {
              this.$message({
                type: "error",
                message: `注意:${res.data.msg}`,
              });
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
#particles-js {
  width: 100%;
  height: 100%;
  background-color: rgba(7, 0, 24, 0.992);
  position: absolute;
}
.box {
  position: relative;
  width: 500px;
  height: 315px;
  margin: auto;
  top: 150px;
  border-radius: 15px;

  background-color: rgba(255, 255, 255, 0.304);
}
.form {
  display: block;
  width: 60%;
  position: absolute;
  top: 15%;
  left: 20%;
}
.form .title {
  text-align: center;
  font-size: 2em;
  color: rgba(255, 255, 255, 0.652);
  margin-bottom: 20px;
}
</style>
